#include<iostream>
#include<cstdlib>
#include<stack>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
	vector<int>numstk;
	string chstk;
	void CalcNum() {
		int num1, num2, num;
		char ch;
		if (numstk.size() == 1)
			return;
		num1 = numstk.back();
		numstk.pop_back();
		num2 = numstk.back();
		numstk.pop_back();
		ch = chstk.back();
		chstk.pop_back();
		switch (ch) {
		case '+':num = num2 + num1; break;
		case '-':num = num2 - num1; break;
		}
		numstk.push_back(num);
	}
	void GetNum(string& s, int& i, int& num) {
		while (i < s.length() && s[i] >= '0'&&s[i] <= '9')
			num = num * 10 + (s[i++] - '0');
		i--;
		numstk.push_back(num);
		num = 0;
	}
	int calculate(string s) {
		int num = 0;
		s.push_back(')');
		chstk.push_back('(');
		for (int i = 0; i < s.length(); i++) {
			if (s[i] >= '0'&&s[i] <= '9')
				GetNum(s, i, num);
			else if (s[i] == ')') {
				while (chstk.back() != '(')
					CalcNum();
				chstk.pop_back();
			}
			else 
				chstk.push_back(s[i]);
		}
		return numstk.back();
	}
};
int main() {
	Solution s;
	cout << s.calculate("-2+ 1") << endl;
	system("pause");
	return 0;
}
